package com.lhf.legalPlant.controller;

import com.lhf.enumfile.Status;
import com.lhf.legalPlant.model.*;
import com.lhf.legalPlant.service.FactoryService;
import com.lhf.planClass.model.StatusVo;
import com.lhf.util.ResultBuliderVo;
import com.lhf.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 尐痞猫
 * @date2021-06-25 22:48
 */
@RestController
@CrossOrigin
public class FactoryController {

    @Autowired
    private FactoryService factoryService;

    //法人下拉框
    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo selectByUserId(String _) {
        //正常逻辑这里应该通过session来获取User登录的信息后发送后台查询属下法人
        User user = new User(1, "zhangsan", "123");
        return factoryService.selectByUserId(user);
    }

    //分页查询
    @PostMapping("/legalPlant/query")
    public ResultVo query(@RequestBody FactorySelectVo factorySelectVo) {
        return factoryService.query(factorySelectVo);
    }

    //add
    @PostMapping("/legalPlant/add")
    public ResultVo add(@Valid @RequestBody  FactoryAddVo factoryAddVo, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ResultBuliderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return factoryService.add(factoryAddVo);
    }
    //update
    @PostMapping("/legalPlant/update")
    public ResultVo update(@Valid @RequestBody FactoryUpdateVo factoryUpdateVo, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ResultBuliderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return factoryService.update(factoryUpdateVo);
    }
    //updateStatus
    @PostMapping("/legalPlant/updatestatus")
    public ResultVo updateStatus(@Valid @RequestBody FactoryUpdateStatusVo factoryUpdateStatusVo, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ResultBuliderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return factoryService.updateStatus(factoryUpdateStatusVo);
    }

    //是否有启用工厂
    @PostMapping("/dsadqueryFactory")
    public ResultVo queryFactory(@RequestBody LegelPersonVo legelPersonVo){
        return factoryService.queryFactory(legelPersonVo);
    }

    @PostMapping("dsadsametadata/getPullDownList")
    public ResultVo xiala(){
        List<StatusVo> statusVos = new ArrayList<>();

        for (Status c : Status.values()) {
            statusVos.add(new StatusVo(c.getStatusName(),c.getStatus()));
        }
        return ResultBuliderVo.success(statusVos);
    }

}
